package com.gxuwz.leaveSystem.dao;

import com.gxuwz.leaveSystem.entity.Student;
import com.gxuwz.leaveSystem.entity.User;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;

import java.util.List;

public class UserDao {
    /**
     * 访问用户数据类
     * 定义jdbctemplate属性及其setter方法
     */
    private JdbcTemplate jdbcTemplate;
    public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
        this.jdbcTemplate = jdbcTemplate;
    }

    /**
     * 添加用户
     * @param user
     * @return
     */
    public int addUser(User user){
        String sql = "insert into sys_user(userid,fullname,password,telephone,userType) value(?,?,?,?,?)";
        Object[] obj = new Object[] {
                user.getUserid(),
                user.getFullname(),
                user.getPassword(),
                user.getTelephone(),
                user.getUserType()
        };
        int num = this.jdbcTemplate.update(sql,obj);
        return num;
    }

    /**
     * 删除账户
     * @param
     * @return
     */
    public int deleteUser(String id){
        String sql = "delete from sys_user where userid=?";
        int num = this.jdbcTemplate.update(sql,id);
        return num;
    }


    /**
     * 更新用户
     * @param user
     * @return
     */
    public int updateUser(User user){
        String sql = "update sys_user set fullname=?,password=?,telephone=?,userType = ? where userid=?";
        Object[] up = new Object[] {
                user.getFullname(),
                user.getPassword(),
                user.getTelephone(),
                user.getUserType(),
                user.getUserid()
        };
        int num = this.jdbcTemplate.update(sql,up);
        return num;
    }



    /**
     * 获取单个账户信息
     * @param id
     * @return
     */
    public User findUserByid(String id){
        String sql = "select * from sys_user where userid = ?";
        //通过rowmapper返回单行记录
        RowMapper<User> rowMapper = new BeanPropertyRowMapper<User>(User.class);
        // 使用query方法返回一个List
        List<User> users = this.jdbcTemplate.query(sql, rowMapper, id);
        // 检查List是否为空
        if (users.isEmpty()) {
            return null; // 或者返回一个默认的User对象
        } else {
            return users.get(0);
        }
    }

    /**
     * 获取所有的账户
     * @return
     */
    public List<User> findAllUser(){
        String sql = "select * from sys_user";
        RowMapper<User> rowMapper = new BeanPropertyRowMapper<User>(User.class);
        return  this.jdbcTemplate.query(sql,rowMapper);
    }

    /**
     * 根据关键字搜索
     * @param keyword
     * @return
     */

    public List<User> searchUsers(String keyword) {
        String sql = "SELECT * FROM sys_user WHERE userid LIKE ? OR fullname LIKE ? OR telephone LIKE ? OR userType LIKE ?";
        return jdbcTemplate.query(sql, new Object[]{"%" + keyword + "%","%" + keyword + "%", "%" + keyword + "%", "%" + keyword + "%"}, new BeanPropertyRowMapper<>(User.class));
    }

}
